/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[DialogComponent.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package swing.components;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 * Swing对话框组件
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class DialogComponent {

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				DialogFrame frame = new DialogFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class DialogFrame extends JFrame {

	private static final long serialVersionUID = 2865193634201963674L;

	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;

	private AboutDialog dialog = null;

	public DialogFrame() {
		setTitle("Dialog Test");
		this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setLocationByPlatform(true);
		setResizable(false);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu fileMenu = new JMenu("文件");
		menuBar.add(fileMenu);

		JMenuItem aboutItem = new JMenuItem("关于");
		aboutItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 第一次使用对话框
				if (dialog == null) {
					dialog = new AboutDialog(DialogFrame.this);
				}
				dialog.setVisible(true);
			}
		});
		fileMenu.add(aboutItem);

		JMenuItem exitItem = new JMenuItem("退出");
		exitItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		fileMenu.add(exitItem);
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class AboutDialog extends JDialog {

	private static final long serialVersionUID = -4187582948586477925L;

	public AboutDialog(JFrame owner) {
		super(owner, "About Dialog Demo", true);
		this.add(new JLabel("<html><h1><i>Core Java</i></h1><hr>" + "Tested By Sean</html>"), BorderLayout.CENTER);
		setLocationByPlatform(true);

		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});

		JPanel panel = new JPanel();
		panel.add(okButton);
		this.add(panel, BorderLayout.SOUTH);
		this.setSize(150, 150);// 设置对话框的大小
	}
}